<?php
namespace backend\controllers;


use common\models\AttributeDeion;
use common\models\Goods;
use common\service\ConstantService;
use common\service\GoodsService;
use common\models\GoodsAttr;
use common\models\Tree;
use common\models\GoodsImg;

class GoodsController extends BaseController
{

    /**
     * 列表页
     */
    public function actionIndex(){
        if($this->isGet() && $this->isAjax()){
            $Goods = Goods::find();
            $Goods_name = $this->get('goods_name')?$this->get('goods_name'):'';
            $tree_id = $this->get('one_id')?$this->get('one_id'):0;
            $two_id = $this->get('two_id')?$this->get('two_id'):0;
            $three_id = $this->get('three_id')?$this->get('three_id'):0;
            $Goods->andWhere(['is_del'=>0]);
            if($Goods_name){
                $Goods->andWhere(['like','goods_name',$Goods_name]);
            }
            if($tree_id){
                $Goods->andWhere(['one_tree_id'=>$tree_id]);

            }
            if($two_id){
                $Goods->andWhere(['two_tree_id'=>$two_id]);

            }
            if($three_id){
                $Goods->andWhere(['three_tree_id'=>$three_id]);
            }
            $p = intval( $this->get('page',1) );
            $this->page_size = intval( $this->get('limit',10) );
            $p = ( $p > 0 )? $p : 1;
            $count = $Goods->count();

            $offset = ($p - 1) * $this->page_size;

            $data = $Goods->select('goods_name,status,id,shop_id,sales_volume')
                ->limit($this->page_size)
                ->offset($offset)
                ->orderBy(['id'=>SORT_DESC])
                ->asArray()
                ->all();

            $goodsModel = new Goods();
            $list = $goodsModel->GetgoogsList($data,1);

            return $this->renderPageJson($list,$count,ConstantService::$response_tab_success);
        }
        $trees = Tree::find()->where(['level'=>'1'])->asArray()->all();
        return $this->render('index',['trees' => $trees]);
    }

    /**
     * 查看SKU
     */
    public function actionGetGoodsattr(){
        if($this->isAjax() && $this->isGet()){
            $id = $this->get('id');
            $list = GoodsService::GetAttrlist($id);

            return $this->renderJson($list,'',ConstantService::$response_success);
        }
    }


    /**
     * 查看商品详情
     * @return string|void
     * @throws \yii\db\Exception
     */
    public function actionGetGoodsinfo(){
        $googs_id = $this->get('id');

        if(!$googs_id){
            return $this->renderJson([],'缺少参数',ConstantService::$response_fail);
        }
        $goods = Goods::find()->where(['id'=> $googs_id])->asArray()->one();

        $goodsimg = GoodsImg::find()->select('img_url')->where(['goods_id'=>$googs_id,'status'=>1])->asArray()->all();
        $goods['banner'] = $goodsimg;

        $googsattr = GoodsAttr::find()->where(['goods_id' => $googs_id,'is_del'=>0])->orderBy(['id'=>SORT_ASC])->asArray()->all();
        $onetree = Tree::find()->where(['pid'=>0])->asArray()->all();
        $twotree = Tree::find()->where(['pid'=>$goods['one_tree_id']])->asArray()->all();
        $threetree = Tree::find()->where(['pid'=>$goods['two_tree_id']])->asArray()->all();

//        $attrtitle = AttributeValue::find()->where(['id'=>$googsattr[0]])
        $attrlists = GoodsService::GetAttrlist($googs_id);
        $deion = AttributeDeion::find()
            ->select('id,key,val')
            ->where(['goods_id'=>$googs_id])
            ->asArray()
            ->all();
        return $this->render('get-goodsinfo',[
            'onetree' => $onetree,
            'twotree' => $twotree,
            'threetree' => $threetree,
            'attrlist' => $attrlists,
            'goods' => $goods,
            'googsattr' => $googsattr,
            'attr_des' => $deion,
            'attr_count' => count($googsattr)
        ]);
    }

    /**
     * 商品分类三级联动
     */
    public function actionGetTree(){
        $id = $this->post('id');
        $data = Tree::find()->where(['pid'=>$id])->asArray()->all();
        return $this->renderJson($data,'',ConstantService::$response_success);
    }


}